package com.example.baselibrary.fragment;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.baselibrary.bean.PublicViewHolder;
import com.example.baselibrary.util.BaseResourceUtil;

import cc.vv.lkbasecomponent.base.ui.LKBaseFragment;

/**
 * Created by sun.li on 2018/4/12.
 *
 * @author sun.li
 */

public abstract class BaseFragment extends LKBaseFragment{

    private View view = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(initLayoutRes(), container, false);
        }
        try {
            if(null!=view){
                ViewGroup parent = (ViewGroup)view.getParent();
                if (null != parent) {
                    parent.removeView(view);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    /**
     * 初始化界面布局文件
     */
    protected abstract int initLayoutRes();

    /**
     * 初始化界面资源对象文件
     */
    protected abstract PublicViewHolder initViewHolderObject();

    /**
     * 初始化界面资源对象文件
     */
    protected abstract void baseOnClick(int viewId);

    @CallSuper
    @Override
    protected void onFirstVisible() {
        initViewHolder(initViewHolderObject());
        super.onFirstVisible();
    }

    @CallSuper
    @Override
    protected void initView(Bundle bundle) {

    }

    /**
     * 初始化界面中所有控件
     */
    private void initViewHolder(PublicViewHolder viewHolder) {
        if (null != view) {
            BaseResourceUtil.getInstance().initViewHolderResource(getActivity(), view, viewHolder, new BaseResourceUtil.ViewEventInterface() {
                @Override
                public void viewOnClick(int resId) {
                    baseOnClick(resId);
                }
            });
        }
    }
}
